Skip to content

Name the underlying native thread on all Apple platforms#130180

Open
kotlarmilos with Copilot wants to merge 5 commits into
mainfrom
copilot/clr-ios-fix-no-name-thread
Open

Name the underlying native thread on all Apple platforms#130180
kotlarmilos with Copilot wants to merge 5 commits into
mainfrom
copilot/clr-ios-fix-no-name-thread

Conversation

Copilot AI commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

On startup, the managed thread name is copied to the underlying native thread only where the thread must name itself, and that path was guarded by TARGET_OSX, so on iOS, tvOS and MacCatalyst the native thread kept its default name and native debuggers and tools never saw the managed thread name. This PR updates the guard to TARGET_APPLE so all Apple platforms name their native thread.

Contributes to #130166

Co-authored-by: kotlarmilos <11523312+kotlarmilos@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot July 3, 2026 13:46
…fter release

Co-authored-by: kotlarmilos <11523312+kotlarmilos@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot July 3, 2026 13:51
Copilot AI changed the title [WIP] Fix threads showing 'No Name' in Debug mode Notify debugger when the exposed Thread object is lazily created Jul 3, 2026
Copilot AI requested a review from kotlarmilos July 3, 2026 13:52
@kotlarmilos

Copy link
Copy Markdown
Member

@copilot drop comments

@kotlarmilos kotlarmilos added this to the 11.0.0 milestone Jul 3, 2026
@kotlarmilos kotlarmilos marked this pull request as ready for review July 3, 2026 14:05
Copilot AI review requested due to automatic review settings July 3, 2026 14:05
Co-authored-by: kotlarmilos <11523312+kotlarmilos@users.noreply.github.com>

Copilot AI commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

@copilot drop comments

Dropped the comments in 22c9235.

@copilot drop comments

Correction: the comment removal is in a46e593.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates CoreCLR’s Thread::GetExposedObject (the lazy creator for the managed System.Threading.Thread instance) to notify an attached debugger immediately after the exposed object is successfully created, so the debugger can refresh its view of that thread.

Changes:

  • Track when GetExposedObject is the call that actually creates/stores m_ExposedObject.
  • After GCPROTECT_END() (and thus after releasing the ThreadStore lock), fire g_pDebugInterface->NameChangeEvent(NULL, this) when the current thread created its own exposed object and a debugger is attached.
Show a summary per file
File Description
src/coreclr/vm/threads.cpp Adds a debugger notification after lazy creation of the managed Thread object to prompt a refresh/re-query of thread info.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 0

Copilot AI requested review from Copilot and removed request for Copilot July 3, 2026 14:11
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/vm/threads.cpp Outdated
@jkotas

jkotas commented Jul 3, 2026

Copy link
Copy Markdown
Member

The managed thread name can be only ever set via exposed thread object. How do we end up in a situation where thread has name, but it does not have the managed exposed object in the first place?

Comment thread src/coreclr/vm/threads.cpp
@kotlarmilos

Copy link
Copy Markdown
Member

The managed thread name can be only ever set via exposed thread object. How do we end up in a situation where thread has name, but it does not have the managed exposed object in the first place?

You are right, the debugger only ever reads the name from the managed Thread object through CordbThread::GetObject, and the name can only be set on that same object whose setter already fires NameChangeEvent, so there is no way to have a real name without the exposed object.

My reasoning up to this point was that native-origin threads such as the main thread create the exposed object lazily on the first managed read of Thread.CurrentThread, which happens after the CreateThread event is emitted, so I read the "No Name" shown in the IDE as a name that failed to propagate, which was wrong. So is "No Name" the correct behavior for native-origin threads or is there a case where such a thread is expected to carry a name?

I will fix the thread name in native debugger in this PR only until we understand better the managed case.

/cc: @thaystg @noahfalk

Extend the startup native-thread naming path from TARGET_OSX to
TARGET_APPLE so iOS, tvOS and MacCatalyst also name the underlying
native thread, where only the thread itself can set its own name.
This affects the thread name shown by native debuggers and tools.

Also drop the earlier managed-debugger NameChangeEvent notification,
which did not address the managed "No Name" case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 6, 2026 09:32
@kotlarmilos kotlarmilos changed the title Notify debugger when the exposed Thread object is lazily created Name the underlying native thread on all Apple platforms Jul 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Comment on lines 77 to 80
// On other platforms, when the underlying native thread is created,
// the thread name is set to the name of the managed thread by another thread.
// However, on OS X and NativeAOT (across all OSes), only the thread itself can set its name.
// However, on Apple platforms and NativeAOT (across all OSes), only the thread itself can set its name.
// Therefore, by this point the native thread is still unnamed as it has not started yet.
Comment on lines +76 to 80
#if TARGET_APPLE || NATIVEAOT
// On other platforms, when the underlying native thread is created,
// the thread name is set to the name of the managed thread by another thread.
// However, on OS X and NativeAOT (across all OSes), only the thread itself can set its name.
// However, on Apple platforms and NativeAOT (across all OSes), only the thread itself can set its name.
// Therefore, by this point the native thread is still unnamed as it has not started yet.
@jkotas

jkotas commented Jul 6, 2026

Copy link
Copy Markdown
Member

So is "No Name" the correct behavior for native-origin threads

I think so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clr-ios] Thread shows "No Name" because GetObject returns null at CreateThread time

4 participants